package cn.edu.sctu.sctu_lysd.presenter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import cn.edu.sctu.sctu_lysd.alipay.PayActivity;
import cn.edu.sctu.sctu_lysd.bean.DepositBean;
import cn.edu.sctu.sctu_lysd.bean.JsonBaseBeen;
import cn.edu.sctu.sctu_lysd.bean.OrderData;
import cn.edu.sctu.sctu_lysd.biz.DepositBiz;
import cn.edu.sctu.sctu_lysd.cofig.CacheKeyConfig;
import cn.edu.sctu.sctu_lysd.net.NetService;
import cn.edu.sctu.sctu_lysd.net.RetrofitHelper;
import cn.edu.sctu.sctu_lysd.utils.JumpUtils;
import cn.edu.sctu.sctu_lysd.utils.SharePreferenceUtils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by wangfuxin on 2017-04-19.
 */

public class DepositPresenter implements DepositBiz.Presenter {

    private NetService service;
    private SharePreferenceUtils sharePreferenceUtils;

    private Context context;
    private DepositBiz.View view;

    private static final int DEPOSIT_ERROR_CODE = 40000703;//该用户已缴纳定金


    public DepositPresenter(Context context, DepositBiz.View view) {
        this.context = context;
        this.view = view;
    }

    @Override
    public void start() {

        RetrofitHelper helper = RetrofitHelper.getInstance();

        service = (NetService) helper.getService(NetService.class);

        sharePreferenceUtils = SharePreferenceUtils.getInstance(context);
    }

    @Override
    public void getDepositOrder() {

        Call<JsonBaseBeen<OrderData>> call = service.getDepositOrder(sharePreferenceUtils.getString(CacheKeyConfig.CACHE_ACCESS_TOKEN),"sss");

        call.enqueue(new Callback<JsonBaseBeen<OrderData>>() {
            @Override
            public void onResponse(Call<JsonBaseBeen<OrderData>> call, Response<JsonBaseBeen<OrderData>> response) {

                if (response.code() == 200){

                    if (response.body().getMeta().getCode() == 200){

                        view.getDepositOrderSuccess(response.body().getData().getId());

                        return;
                    }

                    if (response.body().getMeta().getCode() == DEPOSIT_ERROR_CODE){

                        view.userAlreadyDeposit();

                    }

                }

                view.getDepositOrderFailed();

            }

            @Override
            public void onFailure(Call<JsonBaseBeen<OrderData>> call, Throwable t) {

            }
        });


    }

    @Override
    public void intentPay(long orderID) {

        Bundle bundle = new Bundle();

        bundle.putLong(PayActivity.ORDER_ID_INTENT_TAG,orderID);
        bundle.putLong(PayActivity.INTENT_TAG,PayActivity.DEPOSIT_TAG);

        JumpUtils.jumpActivity(context,PayActivity.class,bundle);

    }

    @Override
    public void getDeposit() {
        service.getDeposit().enqueue(new Callback<JsonBaseBeen<DepositBean>>() {
            @Override
            public void onResponse(Call<JsonBaseBeen<DepositBean>> call, Response<JsonBaseBeen<DepositBean>> response) {
                if (response.code() == 200 && response.body().getMeta().getCode() == 200){

                    view.getDepositSuccess(response.body().getData().getAmount());

                }else {

                    view.getDepositOrderFailed();

                }
            }

            @Override
            public void onFailure(Call<JsonBaseBeen<DepositBean>> call, Throwable t) {
                view.getDepositOrderFailed();
            }
        });
    }
}
